package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.NewCombinationInfoDao;
import com.JZ.QM.common.entity.NewCombinationInfoEntity;
import com.JZ.QM.common.entity.NewCombinationShareEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.NewCombinationInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Service("newCombinationInfoService")
public class NewCombinationInfoServiceImpl extends ServiceImpl<NewCombinationInfoDao, NewCombinationInfoEntity> implements NewCombinationInfoService {
    @Resource
    NewCombinationInfoDao newCombinationInfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<NewCombinationInfoEntity> page = this.page(
                new Query<NewCombinationInfoEntity>().getPage(params),
                new QueryWrapper<NewCombinationInfoEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 根据组合id查询用户修改后的组合
     * @param comBaseId 组合的基本信息id
     * @return
     */
    @Override
    public ArrayList<Long> getNewCombinationListsByCombaseId(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //用户修改后的信息列表
        ArrayList<NewCombinationInfoEntity> newCombinationInfoEntities=new ArrayList<>();
        //用户修改后的组合列表id
        ArrayList<Long> newCombinationIds=new ArrayList<>();
        //最多的行数
        int index=0;

        queryWrapper.select("new_com_id")
                .eq("is_used",1);
        queryWrapper.eq("com_base_id",comBaseId);
        newCombinationInfoEntities= (ArrayList<NewCombinationInfoEntity>) newCombinationInfoDao.selectList(queryWrapper);
        //筛选出用户修改的组合id
        for (Iterator<NewCombinationInfoEntity> it = newCombinationInfoEntities.iterator(); it.hasNext() && index<5; ) {
            NewCombinationInfoEntity iter = (NewCombinationInfoEntity) it.next();
            newCombinationIds.add(iter.getNewComId());
            index++;
        }
        return newCombinationIds;
    }

    @Override
    public Long getNewComId(Long userId, Long comBaseId) {
        //修改后组合实体
        NewCombinationInfoEntity newCombinationEntity = new NewCombinationInfoEntity();

        QueryWrapper<NewCombinationInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_base_id",comBaseId)
                    .eq("user_id",userId);

        newCombinationEntity = newCombinationInfoDao.selectOne(queryWrapper);
        if(newCombinationEntity != null){
            return newCombinationEntity.getNewComId();
        }else {
            return comBaseId;
        }
    }

}